package com.ye.cloud.category.controller;

import com.ye.cloud.category.dto.SaveCategoryDto;
import com.ye.cloud.category.dto.UpdateCategoryDto;
import com.ye.cloud.category.entity.CloudCategory;
import com.ye.cloud.category.service.CloudCategoryService;
import com.ye.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: baby
 * @Date: 2023/10/10 11:11
 * @Description:
 */
@Api(value = "类别管理")
@RestController
public class CloudCategoryController {
    @Autowired
    private CloudCategoryService categoryService;


    @ApiOperation(value = "保存类别")
    @PostMapping("/save")
    private R<?> save(@RequestBody SaveCategoryDto dto){
        CloudCategory cloudCategory = new CloudCategory();
        BeanUtils.copyProperties(dto,cloudCategory);
        try {
            categoryService.save(cloudCategory);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @ApiOperation(value = "删除类别")
    @DeleteMapping("/delete/{id}")
    private R<?> delete(@PathVariable Long id){
        try {
            categoryService.removeById(id);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @ApiOperation(value = "修改类别")
    @PutMapping("/update")
    private R<?> update(UpdateCategoryDto dto){
        CloudCategory cloudCategory = new CloudCategory();
        BeanUtils.copyProperties(dto,cloudCategory);
        try {
            categoryService.updateById(cloudCategory);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }


}
